package m.jclecai.com.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import m.jclecai.com.R;

import java.util.Map;

/**
 * Created by hp on 2017/4/22.
 */

public class RecyclerAdapter extends  RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{

    private Context ctxt;
    private String[] dynaLists;
    private String[] types;
    private Map<String, String> map;
    private OnItemClickListener mOnItemClickListener;

    public RecyclerAdapter(Context ctxt, String[] dynaLists, String[] types,Map<String, String> map) {
        this.ctxt = ctxt;
        this.dynaLists = dynaLists;
        this.types = types;
        this.map = map;
    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(ctxt).inflate(R.layout.fragment_hemai_popuwindow_item, null);
        return new ViewHolder(view);

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        String name = dynaLists[position];
        String type = types[position];
        if (map.containsKey(name)) {
            firstCheckView =  holder.txt_hemai_popuwindow;
            firstCheckView
                    .setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg_press);
        } else {
            holder.txt_hemai_popuwindow
                    .setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg);
        }
        name = name.equals("1串1") ? "单关":name;
        holder.txt_hemai_popuwindow.setText(name);
        holder.txt_hemai_popuwindow.setTag(type);

        if (null != mOnItemClickListener) {

        }
        holder.txt_hemai_popuwindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.txt_hemai_popuwindow, position);

            }
        });
    }

    @Override
    public int getItemCount() {
        return dynaLists.length;
    }
    class ViewHolder extends RecyclerView.ViewHolder {

      TextView txt_hemai_popuwindow;

        public ViewHolder(View itemView) {
            super(itemView);
            txt_hemai_popuwindow = (TextView) itemView.findViewById(R.id.txt_hemai_popuwindow);
        }
    }
    public interface OnItemClickListener {
        void onItemClick(TextView v, int position);
    }

    TextView firstCheckView;

    public TextView getFirstCheckView(){
        return firstCheckView;
    }
}
